package org.sgmiller.quickstem.tests;

import junit.framework.TestCase;

import org.sgmiller.quickstem.IsolationLevel;
import org.sgmiller.quickstem.SerializationException;
import org.sgmiller.quickstem.Stem;
import org.sgmiller.quickstem.Transaction;


public class TestSerialization extends TestCase {

	public void testSerialization() {
		final Stem<Integer> v1=new Stem<Integer>(0);
		
		Thread a=new Thread(new Runnable(){

			@Override
			public void run() {
				Transaction.begin(IsolationLevel.SERIALIZABLE);

				v1.set(5);
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
				}
				
				try {
					Transaction.commit();					
				} catch (SerializationException e) {
					fail(e.getMessage());
				}
			}
			
		});

		a.start();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
		}
		
		Thread b=new Thread(new Runnable(){

			@Override
			public void run() {
				Transaction.begin(IsolationLevel.SERIALIZABLE);
				int i = v1.get();
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
				}
				v1.set(i+1);
				
				try {
					Transaction.commit();					
				} catch (SerializationException e) {
					fail(e.getMessage());
				}
			}
			
		});
		b.start();
		try {
			a.join();
			b.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void testStates() {
		Transaction.begin();
		Transaction.begin();
	}
}
